package com.apes.framework.plugin.form.parser.matcher;


import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.log.timer.Timer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 功能：Parentheses表达式模式匹配器
 *
 * @author xul
 * @create 2018-04-19 15:03
 */
public class ParenthesesMatcher extends ConditionalMatcher {

    /**
     * 功能：获取识别模式
     */
    @Override
    public String getRegex() {
        return "(\\()(.+?)(\\))";
    }

    /**
     * 功能：模式配置
     *
     * @param content
     */
    @Override
//    @Timer("解析Domain表达式")
    public List parse(final String content) {
        List<String> expressions = super.parse(content);
        Matcher matcher;
        if (isDomain(this.getParent())) {
            matcher = new FilterMatcher();
        } else {
            matcher = new ExpressionMatcher();
        }
        matcher.setParent(this);
        return expressions.stream().map(expression -> matcher.parse(expression)).collect(Collectors.toList());
    }

    private boolean isDomain(Matcher parent) {
        if (parent instanceof DomainMatcher) return true;
        if (parent.getParent() != null) return isDomain(parent.getParent());
        return false;
    }

}

